LWJGL 이동과 회전(LWJGL - Rotation and Picking)[LWJGL,이동,회전,Rotation and Picking,lwjgl.org,Lightweight Java Game Library]

이미지출처 : www.lwjgl.org

LWJGL 이동과 회전 그리고 픽킹(LWJGL - Rotation and Picking)

물체의 이동과 회전


Code:

GL11.glTranslatef(this.xoff, this.yoff, this.zoff-this.centerAvgXYZ);
        GL11.glRotatef(this.xrot, 1.0f, 0.0f, 0.0f);
        GL11.glRotatef(this.yrot, 0.0f, 1.0f, 0.0f);


Picking


Code:

public void SelectObjects(int x, int y) {
      int hits;
      int[] viewport = new int[4];
      int buffer[] = new int[256];
      IntBuffer selectBuff = ByteBuffer.allocateDirect(1024).order(ByteOrder.nativeOrder()).asIntBuffer();
        IntBuffer vpBuffer = ByteBuffer.allocateDirect(64).order(ByteOrder.nativeOrder()).asIntBuffer();
      GL11.glGetInteger(GL11.GL_VIEWPORT, vpBuffer);
      vpBuffer.get(viewport);
      GL11.glSelectBuffer(selectBuff);
      GL11.glMatrixMode(GL11.GL_PROJECTION);
      GL11.glPushMatrix();
      GL11.glRenderMode(GL11.GL_SELECT);
      GL11.glLoadIdentity();
      GLU.gluPickMatrix((float)x,(float)(viewport[3]-y), 5.0f, 5.0f, viewport);    
        GLU.gluPerspective(45.0f, ratio, 0.001f, 100.0f);
           //Rectangle rect = this.canvas.getClientArea();              
          // ratio = (float) rect.width / (float) rect.height;
     
      GL11.glMatrixMode(GL11.GL_MODELVIEW); // 물체를 그려줄때는 다시 모델뷰로..
      drawScatterPlot();                                  // 물체를 그려주고,
      GL11.glRotatef(this.xrot, 1.0f, 0.0f, 0.0f);  // 그 후에 회전을 해준다.
        GL11.glRotatef(this.yrot, 0.0f, 1.0f, 0.0f);
       
      hits = GL11.glRenderMode(GL11.GL_RENDER);
      System.out.println("Hits : "+hits);
      selectBuff.get(buffer);
      if(hits>0) {
        int selectedObjID = buffer[3];
      int depth = buffer[1];
                      for (int i = 1; i < hits; i++) {
                if (buffer[i * 4 + 1] < (int) depth) {
                  selectedObjID = buffer[i * 4 + 3];
                      depth = buffer[i * 4 + 1];
                }
          }
          ProcessSelect(selectedObjID);
      }
      GL11.glMatrixMode(GL11.GL_PROJECTION);            
      GL11.glPopMatrix();            
      GL11.glFlush();
      GL11.glMatrixMode(GL11.GL_MODELVIEW);              
  }